package util;

import java.util.*;

import objects.Asteroid;

import main.Constants;

import structure.SObject;

public class OldPartition {
	private Vector[][] contents;
	private static final int X_DIVIDE = 100;
	private static final int Y_DIVIDE = 100;
	/*
	public static void main(String[] args) {
		Partition p = new Partition();
		Asteroid a = new Asteroid(new Point(100, 100)) {
			public void update() {
				cords = new Point(cords.getX() + 1, cords.getY() + 1);
			}
		};
		p.add(a);
		for(int i = 0; i < 1000; i++) {
			p.update(a);

			Vector<SObject> stuff = p.getObjects(a.getX() - 10, a.getY() - 10, 20, 20);
			System.out.println(stuff.size() + " " + a.serialize());
		}
	}
	 */

	public OldPartition() {
		contents = new Vector[Constants.MAX_X/X_DIVIDE][Constants.MAX_Y/Y_DIVIDE];
		for(int i = 0; i < contents.length; i++) {
			for(int j = 0; j < contents.length; j++) {
				contents[i][j] = new Vector();
			}
		}
	}

	public void add(SObject s) {
		System.out.println("&&&&");
		System.out.println("Readded " + (int) (s.getX() / X_DIVIDE) + " " + (int) (s.getY() / Y_DIVIDE));
		contents[(int) (s.getX() / X_DIVIDE)][(int) (s.getY() / Y_DIVIDE)].add(s);

		for(int i = 0 ; i < 10; i ++) {
			for(int j = 0; j < 10; j ++) {
				if(contents[i][j].size() > 0) {
					System.out.println("Info here : " + i + " " + j + " " + ((SObject) contents[i][j].get(0)).toString());
				}
			}
		}
		System.out.println("&&&");
	}

	public void update(SObject s) {
		System.out.println("Start update");
		remove(s);
		s.update();
		add(s);
		System.out.println("End update");
	}

	public void remove(SObject s) {
		contents[(int) (s.getX() / X_DIVIDE)][(int) (s.getY() / Y_DIVIDE)].remove(s);
	}

	public Vector<SObject> getObjects(double x, double y, double width, double height, boolean debug) {
		if(debug) {
			for(int i = 0 ; i < 10; i ++) {
				for(int j = 0; j < 10; j ++) {
					if(contents[i][j].size() > 0) {
						System.out.println("Info here : " + i + " " + j + " " + ((SObject) contents[i][j].get(0)).toString());
					}
				}
			}
		}

		Vector<SObject> results = new Vector<SObject>();
		if(debug)
			System.out.println("******");
		for(int i = Math.max((int) (x / X_DIVIDE), Constants.MIN_X); i <= (x + width) / X_DIVIDE; i++) {
			for(int j = Math.max((int) (y / Y_DIVIDE), Constants.MIN_Y); j <= (y + height) / Y_DIVIDE; j++) {
				if(debug)
					System.out.println("Scanning " + i + " " + j);
				for(Object s : contents[i][j]) {
					if(debug)
						System.out.println("Object here");
					SObject o = (SObject) s;
					if(o.getX() > x && o.getY() > y && o.getX() < x + width && o.getY() < o.getY() + height) {
						results.add(o);
					}
				}
			}
		}
		return results;
	}
}


